let type = '1' //1: thinkphp5 2: we7
let siteInfo = {
  "uniacid": "30",
  "version": "1.20",
  // "siteroot": "http://football.com:8000/index.php/admin/", 
  "siteroot": location.origin+"/index.php/admin/", //线上版本
}

export function reBuildUrl (url) {
    if(type==1) {
        let baseUrl = siteInfo.siteroot
        baseUrl += url;
        return baseUrl;
    }else{
        let arr = url.split('/');
        let uniacid = window.uniacid;
        let baseUrl = window.location.origin+window.location.pathname+'?i='+uniacid+'&c=site&a=entry&m=zkhb_group';
        baseUrl += '&do='+arr[0];
        if(arr[1]){
        	baseUrl += '&op='+arr[1];
        }
        return baseUrl;
    }
}

function request(url, method, data, showLoading=true){
    return new Promise((resolve, reject)=>{
		if(showLoading){
			var loading = window.app.$loading({
							  lock: true,
							  text: '加载中...',
							  spinner: 'el-icon-loading',
							  background: 'rgba(0, 0, 0, 0.7)'
							});
		}
        uni.request({
            url: reBuildUrl(url),
            data: data || {},
            method: method || 'GET',
            success (res) {
				if(showLoading){
					loading.close();
				}
                if(res.statusCode==200){
					if(res.data.code==2) {
						console.log(res.data)
						window.location = '/admin/login'
					}else{
						resolve(res.data);
					}
                }else{
                    var title = res.errMsg;
                    showToast(title);  //提示信息
                    reject();
                } 
            },
			fail() {
				if(showLoading){
					uni.hideLoading();
				}
			}
        });
    })
}



//toast提示信息
export function showToast(title, icon='none'){
    uni.showToast({
        title: title.toString(),
        icon
    });
}

//modal
export function showModal(content){
	uni.showModal({
		content: content
	});
}

//提示功能开发中...
export function showTipPending(){
	showToast('功能开发中...');
}

export function get(url, data, showLoading=true){
    return request(url, 'GET', data, showLoading);
}

export function post(url, data, showLoading=true){
    return request(url, 'POST', data, showLoading);
}


//根据key获取storage
export function getLocalStorage(key){
    return uni.getStorageSync(key);
}

//根据key删除storage
export function removeLocalStorage(key){
    return uni.removeStorageSync(key);
}

//根据key value设置storage
export function setLocalStorage(key,value){
    uni.setStorageSync(key,value);
}

//将日期小于10的转为两位数，  9-> 09
export function numberDouble(num){
    return num<10? '0'+num: num;
}

//将时间戳转为日期时间
export function forMatDate(date,default_val=true){  //中国标准时间对象
    var year=date.getFullYear();
    var month=numberDouble(date.getMonth()+1);
    var dat=numberDouble(date.getDate());
    var hours=numberDouble(date.getHours());
    var min=numberDouble(date.getMinutes());
    var sen=numberDouble(date.getSeconds());
    if(default_val){
        date=year+'-'+month+'-'+dat+' '+hours+':'+min+':'+sen;
    }else{
        date=year+month+dat+hours+min+sen;
    }
    return date;
}


//复制数据
export function copyClipBoardData(data){
	return new Promise((resolve,reject)=>{
		uni.setClipboardData({
			data: data,
			success() {
				showToast('复制成功','success');
				resolve();
			},
			fail(){
				showToast('复制失败，请重试');
			}
		});
	});
}


//上传单张图片
export function uploadFile(file){
	uni.showLoading({
		title: '上传中...'
	});
	return new Promise((resolve,reject)=>{
		uni.uploadFile({
			url: reBuildUrl(uploadFileUrl),
			filePath: file,
			name: 'file',
			formData: {},
			header: {  
			    "Content-Type": "multipart/form-data"  
			},  
			success(res) {
				//console.log(res);
				uni.hideLoading();
				if(res.statusCode==200){
					let data = JSON.parse(res.data);
					if(data.code==0){
						resolve({msg: 'ok', data: data.data});
					}else{
						resolve({msg: 'fail'});
					}
				}else{
					resolve({msg: 'fail'});
				}
			},
			fail(err){
				console.log(err);
				resolve({msg: 'fail'});
			}
		});
	});
};

//上传多张图片
export function uploadMulFiles(files){
	return new Promise((resolve,reject)=>{
		let filesLen = files.length;
		let count = 0;
		let pathsArr = [];  //返回的图品地址
		upload();
		function upload(){
			if(count < filesLen){
				uploadFile(files[count])
				.then((res)=>{
					if(res.msg=='ok'){
						pathsArr.push(res.data);
						count++;
						upload();
					}else{
						resolve({msg: 'fail'});
					}
				});
			}else{
				uni.hideLoading();
				resolve({msg: 'ok', data: pathsArr});
			}
		}
	});
}



//检查手机号
export function checkPhone(phone){
	if(!phone){
		return false;
	};
	if(!/^1\d{10}$/g.test(phone)){
		return false;
	};
	return true;
}